import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:leader_help/page/task/main/task/index.dart';
import 'package:leader_help/utils/util.dart';

class TaskSelectMenuView extends GetView<TaskController> {
  const TaskSelectMenuView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<TaskController>(
      builder: (_) => Container(
        margin: EdgeInsets.all(15.r),
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(5.r),
            boxShadow: [
              BoxShadow(
                  color: Colors.grey.withAlpha(80), offset: const Offset(2, 2)),
              BoxShadow(
                  color: Colors.grey.withAlpha(60), offset: const Offset(3, 3)),
              BoxShadow(
                  color: Colors.grey.withAlpha(50), offset: const Offset(4, 4)),
              BoxShadow(
                  color: Colors.grey.withAlpha(40), offset: const Offset(5, 5))
            ]),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            Expanded(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [1, 2, 3, 4]
                    .asMap()
                    .map(
                      (key, value) => MapEntry(
                        key,
                        getItem(
                          text: _getText(key),
                          isActive: controller.state.activeIndex == key,
                          callback: () => controller.actionMenuTypes(key),
                        ),
                      ),
                    )
                    .values
                    .toList(),
              ),
            ),
            Expanded(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: ['空间分布', '行业分布', '行业空间分布']
                    .asMap()
                    .map(
                      (key, value) => MapEntry(
                        key,
                        InkWell(
                          onTap: () {
                            controller.state.distributeType = key;
                            controller.update();
                          },
                          child: Container(
                            alignment: Alignment.center,
                            child: Text(
                              value,
                              style: pfMediumStyle(
                                  color: key == controller.state.distributeType
                                      ? ColorsUtil.red
                                      : ColorsUtil.title,
                                  fontSize: 13.sp),
                            ),
                          ),
                        ),
                      ),
                    )
                    .values
                    .toList(),
              ),
            ),
          ],
        ),
      ),
    );
  }

  String _getText(int index) {
    switch (index) {
      case 0:
        return controller.state.comTypes[controller.state.selectComTypeIndex];
      case 1:
        return controller.state.selectedMod.areaItem?.name ?? '河南全省';
      case 2:
        return controller.state.industrySelectedMod.item?.name ?? '所有行业';
      case 3:
        return controller.state.scopes[controller.state.selectScopeIndex];
      default:
        return '';
    }
  }

  Widget getItem({
    required String text,
    bool isActive = false,
    VoidCallback? callback,
  }) {
    return InkWell(
      onTap: callback,
      child: Container(
        alignment: Alignment.center,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              constraints: BoxConstraints(maxWidth: (Get.width - 30.r) / 4),
              child: Text(
                text,
                overflow: TextOverflow.ellipsis,
                maxLines: 1,
                style: pfMediumStyle(
                    color: isActive ? ColorsUtil.red : ColorsUtil.title,
                    fontSize: 14.sp),
              ),
            ),
            Icon(
              isActive ? Icons.arrow_drop_up : Icons.arrow_drop_down,
              color: isActive ? ColorsUtil.red : ColorsUtil.content,
              size: 20.r,
            )
          ],
        ),
      ),
    );
  }
}
